﻿using log4net;
using log4net.Config;
using log4net.Core;
using log4net.Repository;
using System;
using System.Diagnostics;
using System.IO;
using System.Text;

namespace MuXue.Zyiz.Template.Common
{
    /// <summary>
    /// 尽量少用这个类，推荐使用注入方法，在构造函数里添加logger
    /// </summary>
    public class LogHelper
    {

        private static ILoggerRepository repository { get; set; }
        private static ILog _log;
        private static ILog log
        {
            get
            {
                try
                {
                    if (_log == null)
                    {

                        Configure();
                    }
                    return _log;
                }
                catch (Exception ex)
                {
                    return null;
                }

            }
        }

        public static void Configure(string repositoryName = "NETCoreRepository", string configFile = "Config/log4net.config")
        {
            try
            {
                repository = LogManager.CreateRepository(repositoryName);
                XmlConfigurator.Configure(repository, new FileInfo(configFile));
                _log = LogManager.GetLogger(repositoryName, repositoryName);
            }
            catch (LogException ex)
            {
                //没有数据 
                _log = LogManager.GetLogger(repositoryName, repositoryName);
            }
            catch (Exception ex2)
            {
                //出现异常

            }


        }



        /// <summary>
        /// 
        /// </summary>
        /// <param name="message"></param>
        /// <param name="e"></param>

        public static void Debug(string message, Exception e = null)
        {
            if (log != null)
            {
                log.Debug(GetCurrentMethodFullName() + " " + message, e);
            }

        }

        public static void Info(string message, Exception e = null)
        {
            if (log != null)
            {
                log.Info(GetCurrentMethodFullName() + " " + message, e);
            }
        }
        public static void Warn(string message, Exception e = null)
        {
            if (log != null)
            {
                log.Warn(GetCurrentMethodFullName() + " " + message, e);
            }
        }
        public static void Error(string message, Exception e = null)
        {
            if (log != null)
            {
                log.Error(GetCurrentMethodFullName() + " " + message, e);
            }
        }
        public static void Fatal(string message, Exception e = null)
        {
            if (log != null)
            {
                log.Fatal(GetCurrentMethodFullName() + " " + message, e);
            }
        }
        private static string GetCurrentMethodFullName()
        {
            try
            {

                StringBuilder sb = new StringBuilder();
                StackTrace stackTrace = new StackTrace();
                return string.Concat(stackTrace.GetFrame(2).GetMethod().DeclaringType.ToString(), ".", stackTrace.GetFrame(2).GetMethod().Name);
            }
            catch
            {
                return "";
            }
        }
    }
}
